package com.joker.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.joker.discovery.InformationDiscovery;
import com.joker.entity.ChatSession;
import com.joker.entity.SessionAndUser;
import com.joker.entity.User;
import com.joker.mapper.ChatSessionMapper;
import com.joker.service.ChatSessionService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 会话表 服务实现类
 * </p>
 *
 * @author joker
 * @since 2021-06-03
 */
@Service
public class ChatSessionServiceImpl implements ChatSessionService {
    @Autowired
    private ChatSessionMapper chatSessionMapper;

    @Autowired
    private InformationDiscovery informationDiscovery;

    @Override
    public ChatSession findBySessionId(int sessionId) {
        return chatSessionMapper.selectById(sessionId);
    }

    @Override
    public List<ChatSession> findByUserId(String userId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id",userId);
        return chatSessionMapper.selectList(queryWrapper);
    }

    @Override
    public List<SessionAndUser> findAllByUserId(String userId) {
        List<SessionAndUser> sessionAndUserList = new ArrayList<>();
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id",userId);
        List<ChatSession> chatSessionList = chatSessionMapper.selectList(queryWrapper);
        System.out.println(chatSessionList);
        if(chatSessionList.size() != 0){
            for(ChatSession chatSession : chatSessionList){
                SessionAndUser sessionAndUser = new SessionAndUser();
                sessionAndUser.setSessionId(chatSession.getSessionId());
                sessionAndUser.setUserId(chatSession.getUserId());
                sessionAndUser.setUserName(informationDiscovery.instances(chatSession.getUserId()).getUserName());
                sessionAndUser.setToUserId(chatSession.getToUserId());
                sessionAndUser.setToUserName(informationDiscovery.instances(chatSession.getToUserId()).getUserName());
                sessionAndUser.setUnReadCount(chatSession.getUnReadCount());
                sessionAndUserList.add(sessionAndUser);
            }
        }
        return sessionAndUserList;
    }

    @Override
    public ChatSession findByUserIdAndToUserId(String userId, String toUserId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id",userId);
        queryWrapper.eq("to_user_id",toUserId);
        ChatSession chatSession = chatSessionMapper.selectOne(queryWrapper);
        return chatSession;
    }

    @Override
    public Integer save(ChatSession chatSession) {
        return chatSessionMapper.insert(chatSession);
    }

    @Override
    public Integer setUnReadCount(String userId, String toUserId) {
        return chatSessionMapper.setUnReadCountByUserIdAndToUserId(userId,toUserId);
    }

    @Override
    public Integer deleteById(int sessionId) {
        return chatSessionMapper.deleteById(sessionId);
    }

    @Override
    public void addUnReadCountByUserIdAndToUserId(String userId, String toUserId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id",userId);
        queryWrapper.eq("to_user_id",toUserId);
        ChatSession chatSession = chatSessionMapper.selectOne(queryWrapper);
        Integer count = chatSession.getUnReadCount();
        count++;
        chatSession.setUnReadCount(count);
        chatSessionMapper.updateById(chatSession);
    }
}
